module.exports = function ($scope, $state, $resource, $modal, toaster, $stateParams) {
	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadlist();
		}
  };
	/* 分页
   * ========================================= */
  $scope.maxSize = 5;            //最多显示多少个按钮
  $scope.bigCurrentPage = 1;      //当前页码
  $scope.itemsPerPage = 10; 
  $scope.searchform = {};
	$scope.loadlist = function (str) {
    var para = {
        pageNo:$scope.bigCurrentPage, 
        pageSize:$scope.itemsPerPage,
    };
    para = angular.extend($scope.searchform, para);
		$resource('/api/as/mc/apply/findLevelList', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
        $scope.objs = res.data.results;
        $scope.bigTotalItems = res.data.totalRecord;
        angular.forEach($scope.objs, function(item){
					if(item.apply_state == 1){
						item.selected = true;
					}else{
						item.selected = false;
					}
				});
			} else {
				toaster.error({ title: '', body: res.errmsg });
			}
		});
	}

  $scope.loadlist();
  
  //设置首页显示不显示
	$scope.change = function(info) {
      console.log(info.selected);
      var url = info.selected ? '/api/as/mc/apply/updateApplyState' : '/api/as/mc/apply/updateApplyStateNot';
      var para = {
        'id': info.id
      }
      $resource(url, {}, {}).save(para, function(res){
        if(res.errcode === 0){
          toaster.success({title: '', body: '设置成功。'});
          $scope.loadlist();
        }else{
          toaster.error({title: '', body: res.errmsg});
        }
      });
	}


  $scope.setStock = function (obj){
    console.log(obj);
        var modalInstance = $modal.open({
          template: require('../views/skwxSetStock.html'),
          controller: 'skwxSetStock',
          // size: 'sm',
          resolve: {
            id: function () {
              return obj.id;
            }
          }
        });
        modalInstance.result.then(function () {
          $scope.loadlist();
        });
  }

};